﻿using System;
using System.Windows.Forms;

namespace LightController.Frm
{
    public partial class Dlg_AddLightController : Form
    {
        ///// <summary>
        ///// 光源控制器的类型
        ///// </summary>
        //private LIGHT_TYPE _TYPE = LIGHT_TYPE.OPT;
        ///// <summary>
        ///// 光源控制器的通道数量
        ///// </summary>
        //private int _chs = 4;

        //private string _name;
        //private string _comAddress;

        //public LIGHT_TYPE TYPE
        //{
        //    get
        //    {
        //        return _TYPE;
        //    }
        //}
        //public int Chs
        //{
        //    get
        //    {
        //        return _chs;
        //    }
        //}
        //public string ControllerName
        //{
        //    get
        //    {
        //        return _name;
        //    }
        //}

        //public string ComAddress
        //{
        //    get
        //    {
        //        return _comAddress;
        //    }
        //}

        private ILightController lightController;

        public ILightController LightController { get { return lightController; } set { lightController = value; } }

        public Dlg_AddLightController()
        {
            InitializeComponent();
        }

        private void Dlg_AddLightController_Load(object sender, EventArgs e)
        {
            if (cb_LightType.Items.Count > 0)
            {
                cb_LightType.SelectedIndex = 0;
            }

        }

        private void btn_Confirm_BtnClick(object sender, EventArgs e)
        {
            string com = tb_CommunicationAddress.Text.Trim();
            if (com.Contains("."))
            {
                string[] ips = com.Split('.');

                byte[] ipAddress = new byte[4];
                if (!byte.TryParse(ips[0], out ipAddress[0]) ||
                    !byte.TryParse(ips[1], out ipAddress[1]) ||
                    !byte.TryParse(ips[2], out ipAddress[2]) ||
                    !byte.TryParse(ips[3], out ipAddress[3]))
                {
                    MessageBox.Show("IP地址格式不正确！");
                    return;
                }
            }
            else if (com.ToLower().Contains("com"))
            {
                string[] c = com.ToUpper().Split('M');
                int comPort = 0;
                if (!int.TryParse(c[1], out comPort))
                {
                    MessageBox.Show("IP地址格式不正确！");
                    return;
                }
            }
            else
            {
                MessageBox.Show("通讯地址格式不正确！");
                return;
            }
            if (cb_LightType.Text == "OPT")
            {
                LightController = new OPTController();
                LightController.ChanelCount = (int)num_LightCHs.Value;
                for (int i = 1; i <= LightController.ChanelCount; i++)
                {
                    LightChanel lightChanel = new LightChanel();
                    lightChanel.Name = "CH" + i;
                    lightChanel.No = i;
                    lightController.ChanelList.Add(lightChanel);
                }
            }
            else if (cb_LightType.Text == "Rsee")
            {
                LightController = new RseeLightController();
                LightController.ChanelCount = (int)num_LightCHs.Value;
                for (int i = 0; i < LightController.ChanelCount; i++)
                {
                    LightChanel lightChanel = new LightChanel();
                    lightChanel.Name = "CH" + i;
                    lightChanel.No = i;
                    lightController.ChanelList.Add(lightChanel);

                }
            }

            LightController.Name = tb_Name.Text.Trim();
            LightController.CommunicationAddress = tb_CommunicationAddress.Text.Trim();
            this.DialogResult = DialogResult.OK;
        }

            private void cb_LightType_SelectedIndexChanged(object sender, EventArgs e)
            {
                tb_Name.Text = cb_LightType.Text;
            }
        }
    }
